package com.twu.biblioteca;

import java.util.*;

public class Biblioteca {

    private BooksLibrary booksLib;

    private MoviesLibrary moviesLib;

    private UserAccounts accounts;

    private UserAccount sessionUser = null;


    protected Biblioteca() {
          booksLib = new BooksLibrary();
          moviesLib = new MoviesLibrary();
          accounts = new UserAccounts();
    }

    protected boolean mainMenuInterface() {

        Vector<String>menuContent=new Vector<String>();
        menuContent.add(0,"********* Library Menu **********") ;
        menuContent.add(1,"View and Reserve Books");
        menuContent.add(2,"View Account");
        menuContent.add(3,"Movies");
        menuContent.add(4,"Exit");

        if(sessionUser == null)
            menuContent.add(4,"Login");
        else
            menuContent.add(4,"Logout");

        System.out.println();
        System.out.println(menuContent.firstElement());
        for(int i=1;i < menuContent.size();i++)
            System.out.print(i + "." + menuContent.elementAt(i) + "\t");
        System.out.println();

        System.out.print("Choose an option : ");
        Scanner sc=new Scanner(System.in);
        int choice;
        choice=sc.nextInt();
        return mainMenuController(choice);

    }

    private boolean mainMenuController(int option) {
        switch(option){
            case 1 :
                      boolean viewBooksMenuAgain;
                      do
                         viewBooksMenuAgain = viewReserveBookMenuInterface();
                      while(viewBooksMenuAgain);

                      break;

            case 2 :  viewAccountController();
                      break;

            case 3:  boolean viewMoviesMenuAgain;
                     do
                        viewMoviesMenuAgain=moviesMenuInterface();
                     while(viewMoviesMenuAgain);

                     break;

            case 4 : if(sessionUser == null)
                           loginController();
                     else
                           sessionUser = null;
                     break;

            //Exit from Main Menu
            case 5 : return false;

            default :System.out.println("Select a valid option!!");
        }
        //Continue with Main Menu
        return true;
    }

    private void viewAccountController() {

     if(sessionUser == null){
        System.out.println("Please talk to Librarian.Thank you");
     }

     else {
        String name = "Tushar Garg";
        String email= "garg.tushar.05@gmail.com";
        String contactNo="9891508667";
        System.out.println(name + email + contactNo);
     }
    }

    private boolean viewReserveBookMenuInterface() {

        booksDisplayInterface();
        System.out.println("1.Reserve a book\t2.Go back to previous menu");
        System.out.print("Chooose an option : ");

        Scanner sc=new Scanner(System.in);
        int option=sc.nextInt();
        return viewReserveBookMenuController(option);
    }

    private void booksDisplayInterface() {

        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("~~~~~~~~~~ Books ~~~~~~~~~~~~");
        Vector<String> booksDetails=new Vector<String>(booksLib.getAllItemDetails());
        for(int i=0;i<booksDetails.size();i++)
            System.out.println( (i+1) + "." +booksDetails.elementAt(i));
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    }

    private boolean viewReserveBookMenuController(int option) {

        switch(option){

            case 1  : if(sessionUser == null) {
                        System.out.println("You must be logged in to reserve a book !");
                        System.out.println("Do you want to log in ? (1 : yes/0 : no) ");
                        Scanner sc = new Scanner(System.in);
                        int choice = sc.nextInt();
                        if(choice == 1) {
                              if( loginController()==true ) {

                                  //Display the same menu again to choose a book
                                  viewReserveBookMenuController(1);
                              }


                        }
                      }

                      else {

                      System.out.print("Enter the book id : ");
                      Scanner sc=new Scanner(System.in);
                      booksLib.reserveBook(sc.nextInt());

                      }
                      break;

            // Exit from View and Reserve Book Menu
            case 2  :  return false;

            default :  System.out.println("Select a valid option !!");
                break;
        }
        //Continue with View and Reserve Book Menu
        return true;

    }

    private boolean moviesMenuInterface() {


        moviesDisplayInterface();

        Vector<String>movieMenuItems=new Vector<String>();
        movieMenuItems.add("Return to Main Menu");
        for(int i=0;i<movieMenuItems.size();i++)
            System.out.print( (i+1) + "." + movieMenuItems.elementAt(i) + "\t");
        System.out.println();
        System.out.print("Choose an option : ");
        Scanner sc=new Scanner(System.in);
        int option=sc.nextInt();

        return moviesMenuController(option);

    }

    private void moviesDisplayInterface() {
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println("~~~~~~~~~~ Movies ~~~~~~~~~~~~");
        Vector<String> moviesDetails = moviesLib.getAllItemDetails();
        System.out.println("Title" + "\t" + "Year" +"\t" + "Director" + "\t" + "Rating");
        System.out.println();
        for(String movie : moviesDetails)
            System.out.println(movie);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    }

    private boolean moviesMenuController(int option) {

        switch(option) {
            case 1 : return false;
        }

        return true;
    }

    private boolean loginController() {

        int attempts = 2;

        while (attempts > 0){

            Scanner sc =new Scanner(System.in);
            String username , password;
            System.out.print("Username ? : ");
            username = sc.next();
            System.out.print("Password ? :");
            password = sc.next();
            UserAccount user = new UserAccount(username , password);
            if( accounts.validateUsernamePassword(user) ) {
                sessionUser = user;
                return true;
            }
            attempts-- ;
        }
        return false;
    }

}
